package learnkotlin

import OnClickListener
import TestLambda

object KotlinDemo3 {
    @JvmStatic
    fun main(args: Array<String>) {

        createList();
        createSet()
        createMap();

        lambdaTest1();
    }


    /**
     * ===================Lambda编程 基础知识
     */


    /**
     * ====================集合的创建与遍历
     */

    fun createList() {
        //创建一个不可变集合
        val list = listOf<String>("apple", "banana", "orange")

        //创建一个可变集合
        val mutableListOf = mutableListOf<String>("apple", "banana", "orange")
        mutableListOf.add("Watermelon")

        //集合的遍历
        for (fruit in mutableListOf)
            println(fruit)
    }

    fun createSet() {
        val of = setOf<String>("apple", "banana", "orange", "apple")
        val setOf = mutableSetOf<String>("apple", "banana", "orange", "apple")
        setOf.add("watermelon")
        for (fruit in setOf) {
            println("水果set" + fruit)
        }

    }

    fun createMap() {
        val hashMap = HashMap<String, Int>()
        hashMap.put("apple", 1)
        hashMap["banana"] = 2
        hashMap["orange"] = 3

        val i = hashMap["banana"]

        val mapOf = mapOf<String, Int>("apple" to 1, "banana" to 3, "orange" to 4)

        val mutableMapOf = mutableMapOf<String, Int>("apple" to 1, "banana" to 3, "orange" to 4)
        mutableMapOf["watermelon"] = 5

        for ((fruit, num) in mutableMapOf) {
            println(fruit + " 有 " + num + " 个")
        }

    }

    /**
     * ====================集合函数式API  来入门 lambda
     */

    fun lambdaTest1() {
        val fruitList = listOf<String>("apple", "banana", "watermelon", "orange")
        var maxLengthFruit = ""
        for (fruit in fruitList) if (fruit.length > maxLengthFruit.length) maxLengthFruit = fruit
        println("the maxLengthFruit is " + maxLengthFruit)

        /**
         * lambda: {参数名1:参数类型,参数名2:参数类型-> 函数体}
         */
        //step 1:
        val lambda = { fruit: String -> fruit.length }
        fruitList.maxByOrNull(lambda)
        //2
        fruitList.maxByOrNull({ fruit: String -> fruit.length })
        //3
        fruitList.maxByOrNull { fruit: String -> fruit.length }
        //4
        fruitList.maxByOrNull { fruit -> fruit.length }
        //5
        maxLengthFruit = fruitList.maxByOrNull { it.length }.toString()
        println(maxLengthFruit)

        val newList = fruitList.map { it.uppercase() }//
        for (fruit in newList) {
            println(fruit)
        }

        /**
         * filter  过滤
         */
        fruitList.filter { it.length <= 5 }.map { it.uppercase() }

        /**
         *  any 至少存在一个元素满足指定条件   all 是否所有元素都满足指定条件
         */

        val any = fruitList.any { it.length <= 5 }


        //lambda 调用Java方法   接收一个接口 只有一个方法

        //不能new 而是改用 object关键字
        setOnClickListener(object : OnClickListener {
            override fun conClick(str: String?) {
                println(str)
            }
        })
        //简化2
        setOnClickListener { str -> println(str) }
        //简化3
        setOnClickListener { println(it) }

        //一个 接口 一个抽象方法 两个参数的lambda 方式
        setOnTestLambda { str: String, value: Int ->
            val app = str + value
            println(app)
        }


    }

    fun setOnClickListener(lisntener: OnClickListener) {
        val str = "hello listener"
        lisntener.conClick(str)
    }

    fun setOnTestLambda(listener: TestLambda) {
        val str = "hello TestLambda"
        listener.onClick(str, 100);
    }


}